package com.example.tyz1201.myaccount.Activity.Activity.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.support.v7.widget.Toolbar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;


import com.example.tyz1201.myaccount.Activity.Activity.Model.AssumeModel;
import com.example.tyz1201.myaccount.R;

/**
 * Created by tyz1201 on 2016/4/8.
 */
public class WriteActivity extends AppCompatActivity {
    EditText et_todomoney, et_tips;
    float todoMoney = 0;
    String tips, assumeData;
    Button bt_return;
    LinearLayout ll_assume_style;
    Toolbar toolbar;
    ImageView iv_choose_show;
    TextView tv_choose_show;
    int choosedIconId;
    String choosedName = "mewho?";
    RadioButton rb_income, rb_outcome;
    long createTime;
    AssumeModel assumeModel;
    RadioGroup rg_assume;
    int choosedAssumeTypeId;
    String createTimeStr;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_write);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("记一笔");
        toolbar.setTitleTextColor(getResources().getColor(R.color.Black));
        setSupportActionBar(toolbar);

        et_todomoney = (EditText) findViewById(R.id.et_todomoney);
        et_tips = (EditText) findViewById(R.id.et_tips);


        ll_assume_style = (LinearLayout) findViewById(R.id.ll_assume_styly);
        ll_assume_style.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(WriteActivity.this, ChooesedStyleActivity.class);
                startActivityForResult(intent, 1);
            }
        });

        rg_assume = (RadioGroup) findViewById(R.id.rg_assume);

        bt_return = (Button) findViewById(R.id.bt_return);
        bt_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                todoMoney = Float.parseFloat(et_todomoney.getText().toString());
                tips = et_tips.getText().toString();
                createTime = System.currentTimeMillis();
                createTimeStr = createTime+"";

                choosedAssumeTypeId = rg_assume.getCheckedRadioButtonId();
                if (choosedAssumeTypeId == R.id.rb_outcome) {
                    choosedAssumeTypeId = -1;
                } else if (choosedAssumeTypeId == R.id.rb_income) {
                    choosedAssumeTypeId = 1;
                }


                assumeModel = new AssumeModel(tips, todoMoney, createTime, choosedName, choosedAssumeTypeId);
                assumeData = assumeModel.toString();


                Log.d("choosedName", choosedAssumeTypeId + "");

                SharedPreferences sharedPreferences = getSharedPreferences("account", MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();

                editor.putString(createTimeStr, assumeData);
                editor.commit();

                finish();
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        Log.d("hehe ", "???");
        iv_choose_show = (ImageView) findViewById(R.id.iv_choose_show);
        tv_choose_show = (TextView) findViewById(R.id.tv_choose_show);

        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {

                choosedName = data.getStringExtra("iconName");
                tv_choose_show.setText("选择消费类型： " + choosedName);

                choosedIconId = data.getIntExtra("iconId", 0);
                iv_choose_show.setImageResource(choosedIconId);
                iv_choose_show.setColorFilter(getResources().getColor(R.color.Black));
                Log.d("choosedId", "" + choosedIconId);

            }
        }
        super.onActivityResult(requestCode, resultCode, data);

    }

}
